﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Net.Http.Formatting;

namespace PivotData.Tests
{
    [TestClass]
    public class FacetUriTest
    {
        [TestMethod]
        public void TestEqualUri()
        {
            var f1 = new FacetUri() { Name = "1", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };
            var f2 = new FacetUri() { Name = "1", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };

            Assert.IsTrue(f1.Equals(f2), "equal");
        }

        [TestMethod]
        public void TestNEqualUri()
        {
            var f1 = new FacetUri() { Name = "1", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };
            var f2 = new FacetUri() { Name = "2", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestNEqualValueURI()
        {
            var f1 = new FacetUri() { Name = "1", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };
            var f2 = new FacetUri() { Name = "1", URI = new Uri("http://www.cnn.com"), LinkString = "dude" };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestNEqualValueLinkString()
        {
            var f1 = new FacetUri() { Name = "1", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };
            var f2 = new FacetUri() { Name = "1", URI = new Uri("http://www.nytimes.com"), LinkString = "wonk" };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestNEqualNumberOtherObj()
        {
            var f1 = new FacetUri() { Name = "1", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };
            var f2 = new FacetString() { Name = "1", Value = "10" };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestJsonRoundTripNumberConversionJson()
        {
            var fsOrig = new FacetUri() { Name = "hi", URI = new Uri("http://www.nytimes.com"), LinkString = "dude" };
            var formatter = new JsonMediaTypeFormatter();
            var stream = new MemoryStream();

            formatter.WriteToStreamAsync(typeof(FacetAbsBase[]), new FacetAbsBase[] { fsOrig }, stream, content: null, transportContext: null).Wait();
            stream.Position = 0;
            var roundtrippedError = formatter.ReadFromStreamAsync(typeof(FacetAbsBase[]), stream, content: null, formatterLogger: null).Result as FacetAbsBase[];

            Assert.IsNotNull(roundtrippedError);
            Assert.AreEqual(1, roundtrippedError.Length);
            Assert.IsInstanceOfType(roundtrippedError[0], typeof(FacetUri));
            Assert.AreEqual("hi", roundtrippedError[0].Name, "name of fs date");
            Assert.AreEqual("http://www.nytimes.com/", (roundtrippedError[0] as FacetUri).URI.AbsoluteUri, "value of fs uri");
            Assert.AreEqual("dude", (roundtrippedError[0] as FacetUri).LinkString, "value of fs link string");
        }
    }
}
